﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using Mytrip.Localization;

namespace Mytrip.Files.Controllers
{
    [HandleError]
    [Localization]
    public class FileController : Controller
    {
        /*-----------------------------------------------------*/
        FileRepository _fr;
        public FileRepository fr
        {
            get
            {
                if (_fr == null)
                    _fr = new FileRepository();
                return _fr;
            }
        }
        /*  ОСНОВНОЙ КОНТЕНТ  */
        /*------------------Список папок и файлов-------------------*/
        //[Authorize(Roles = "admin")]
        public ActionResult Index(string id)
        {
            if (!String.IsNullOrEmpty(id))
            {
                ViewData["folder"] = fr.Get_folders(id);
                ViewData["file"] = fr.Get_files_folder(id);
            }
            else
            {
                ViewData["folder"] = fr.Get_folders();
                ViewData["file"] = fr.Get_files_folder();
            }
            ViewData["dir"] = id;
            return View();
        }
        [HttpPost]
        public ActionResult Index(string id,HttpPostedFileBase file)
        {
            if (file == null)
                ModelState.AddModelError("file",FileLanguage.select_file_upload);
            if (ModelState.IsValid)
            {
                fr.Upload_file(id, file);
                RedirectToAction("Index", new { id });
            }
            ViewData["folder"] = fr.Get_folders(id);
            ViewData["file"] = fr.Get_files_folder(id);
            ViewData["dir"] = id;
            return View();
        }
        /*-----------------Удаление файла или папки-------------------*/
        //[Authorize(Roles = "admin")]
        public ActionResult Delete(string id, string t)
        {
            string df = id.Remove(id.LastIndexOf("()"));
            if (t == "file")
                fr.Delete_file(id);
            else
                fr.Delete_folder(id);
            return RedirectToAction("Index",new { id = df });
        }
        /*-----------------Создать папку------------------*/
        //[Authorize(Roles = "admin")]
        public ActionResult Create(string id, string name)
        {
            name =id +"()" + name;
            fr.Create_folder(name);
            return RedirectToAction("Index", new { id = id });
        }
        /*--------------Переименовать папку или файл -----------------*/
        //[Authorize(Roles = "admin")]
        public ActionResult Rename(string id)
        {
            ViewData["dir"] = id;
            string df = id.Remove(0, id.LastIndexOf("()") + 2);
            ViewData["name"] = df;
            return View();
        }
        [HttpPost]
        public ActionResult Rename(string name, string id, string t)
        {
            string df = id.Remove(id.LastIndexOf("()"));
            if (t == "folder")
                fr.Rename_folder(name, id);
            else if (t == "file")
                fr.Rename_file(name, id);
            if (df == "")
                return RedirectToAction("Index");
            else
                return RedirectToAction("Index", new { id = df });
        }
    }
}
